/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.site.server;

import java.util.Date;
import java.util.List;

import nl.biz_e.site.client.NewsService;
import nl.biz_e.site.server.dao.NewsItemDao;
import nl.biz_e.site.shared.models.NewsItem;

import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.objectify.Key;

/**
 * Server side mplementation of {@link NewsService}
 * 
 * @author Joost A. Bloemsma
 */
public class NewsServiceImpl extends RemoteServiceServlet implements
		NewsService {
	
	private static final long serialVersionUID = 1L;
	private static final NewsItemDao dao = new NewsItemDao();
	private static MemcacheService memcache = MemcacheServiceFactory
			.getMemcacheService("biz_e_site");
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<Key<NewsItem>> getAllKeys() {
		return dao.getAllKeys();
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<NewsItem> getLastNewsItems(int count) {
		return dao.getLast(count);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<NewsItem> getAll() {
		return dao.getAll();
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public NewsItem getNewsItemByNr(long seqNr) {
		return dao.findSingle("seqNr", seqNr);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public NewsItem saveNewsItem(NewsItem item, String fullText) {
		String shortText = item.getText();
		if ((null == shortText) || (shortText.length() < 1)) {
			if (fullText.length() > 100) {
				shortText = fullText.substring(0, 97) + "...";
			} else {
				shortText = fullText;
			}
			item.setText(shortText);
		}
		long seqNr = item.getSeqNr();
		if (!(seqNr > 0)) {
			item.setSeqNr(getNewSeqNr());
		}
		
		if (item.getDate() == null) {
			item.setDate(new Date());
		}
		
		dao.save(item);
		dao.saveFullText(item, fullText);
		
		return item;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getFullText(NewsItem item) {
		return dao.getFullText(item);
	}
	
	/**
	 * Returns a new, free sequence number.<br/>
	 * This is done atomically by using the {@link MemcacheService}
	 * 
	 * @return
	 */
	private long getNewSeqNr() {
		Long next = memcache.increment("NewsSeqNr", 1);
		if (next == null) {
			long last = dao.getLastSeqNr() + 1;
			next = memcache.increment("NewsSeqNr", 1, last);
		}
		return next;
	}
}
